<?php
/**
 * +----------------------------------------------------------------------
 * | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
 * +----------------------------------------------------------------------
 * | File name: UserLoanRequestObserver.php
 * +----------------------------------------------------------------------
 * | Author: sam
 * +----------------------------------------------------------------------
 * | Description: 用于详细说明此程序文件完成的主要功能，与其他模块或函数的接口、输出值、取值范围、含义及参数间的控制、顺序或依赖等关系。
 * +----------------------------------------------------------------------
 * | Others: 其他内容的说明
 * +----------------------------------------------------------------------
 * | History: 修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简述
 * +----------------------------------------------------------------------
 * | 1.Date: 2019/1/7 Author: sam Modification: 创建
 * +----------------------------------------------------------------------
 */

namespace App\Observers;


use App\Facades\SamLogFacade;
use App\Library\Ding\Ding;
use App\Models\UserGlobal;
use App\Models\UserLoanRequest;
use Illuminate\Support\Facades\App;

class UserLoanRequestObserver
{
    /**
     * Description:更新完成之后
     * Author: Sam
     * DateTime: 2019-03-07 22:43
     * @param UserLoanRequest $loan
     */
    public function saved(UserLoanRequest $loan)
    {

        $changes = $loan->getChanges();
        if(!empty($changes['status'])){
            switch ($changes['status']){
//                case UserLoanRequest::STATUS_APPLY:  //授信记录进入轮询状态
//                    SamLogFacade::debug("触发观察者：授信数据【{$loan->id}】状态变更为". UserLoanRequest::STATUS_APPLY);
//                    $global_data = ['global_status'=>UserGlobal::STATUS_LOAN_REQUEST];  //全局状态借款记录已提交
//                    break;
                case UserLoanRequest::STATUS_WAIT_FOR_RISK:  //授信记录等待风控
                    SamLogFacade::debug("触发观察者：授信数据【{$loan->id}】状态变更为". UserLoanRequest::STATUS_WAIT_FOR_RISK);
                    $global_data = ['global_status'=>UserGlobal::STATUS_LOAN_REQUEST];  //全局状态借款记录已提交
                    break;
                case UserLoanRequest::STATUS_SUCCESS: //授信结果成功
                    SamLogFacade::debug("触发观察者：授信数据【{$loan->id}】状态变更为". UserLoanRequest::STATUS_SUCCESS);
                    $global_data = ['global_status'=>UserGlobal::STATUS_LOAN_REQUEST_SUCCESS];//全局状态借款审核通过
                    break;
                case UserLoanRequest::STATUS_FAILD://授信结果失败
                    SamLogFacade::debug("触发观察者：授信数据【{$loan->id}】状态变更为". UserLoanRequest::STATUS_FAILD."(小黑屋)");
                    $global_data = ['global_status'=>UserGlobal::STATUS_LOAN_REQUEST_FAILED,//全局授信失败
                            'last_request_fail_time'=>date('Y-m-d')];
                    break;
                case UserLoanRequest::STATUS_TIME_OUT://收款超时
                    SamLogFacade::debug("触发观察者：授信数据【{$loan->id}】状态变更为". UserLoanRequest::STATUS_TIME_OUT);
                    $global_data = ['global_status'=>UserGlobal::STATUS_LOAN_TIME_OUT];//全局状态收款超时
                    break;
                case UserLoanRequest::STATS_RESTART://收款完全超时
                    SamLogFacade::debug("触发观察者：授信数据【{$loan->id}】状态变更为". UserLoanRequest::STATS_RESTART);
                    $global_data = ['global_status'=>UserGlobal::STATUS_LOAN_RESTART];//全局收款完全超时
                    break;
                case UserLoanRequest::STATUS_CANCEL://授信取消
                    SamLogFacade::debug("触发观察者：授信数据【{$loan->id}】状态变更为". UserLoanRequest::STATUS_CANCEL);
                    $global_data = ['global_status'=>UserGlobal::STATUS_NOTHING];//全局收款完全超时
                    break;
                case UserLoanRequest::STATUS_RISK_FAILED://风控失败
                    SamLogFacade::debug("触发观察者：授信数据【{$loan->id}】状态变更为". UserLoanRequest::STATUS_RISK_FAILED);
                    $global_data = ['global_status'=>UserGlobal::STATUS_LOAN_REQUEST_FAILED];//全局授信失败
                    break;
                case UserLoanRequest::STATUS_RISK_REJECT://风控拒绝
                    SamLogFacade::debug("触发观察者：授信数据【{$loan->id}】状态变更为". UserLoanRequest::STATUS_RISK_REJECT."(小黑屋)");
                    $global_data = ['global_status'=>UserGlobal::STATUS_LOAN_REQUEST_FAILED,//全局授信失败
                        'last_request_fail_time'=>date('Y-m-d')];
                    break;
                case UserLoanRequest::STATUS_LACK_INFO://缺少信息
                    SamLogFacade::debug("触发观察者：授信数据【{$loan->id}】状态变更为". UserLoanRequest::STATUS_LACK_INFO."(缺少信息)");
                    $global_data = ['global_status'=>UserGlobal::STATUS_LOAN_REQUEST_FAILED];
                    break;
            }
            if(!empty($global_data)){
                UserGlobal::where('uid',$loan->user_id)->update($global_data);
            }
        }
    }

    /**
     * Description:创建完成之后
     * Author: Sam
     * DateTime: 2019-03-06 14:56
     * @param UserLoanRequest $loan
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function created(UserLoanRequest $loan)
    {
        $count = UserLoanRequest::where(['user_id'=>$loan->user_id,'current'=>UserLoanRequest::CURRENT_NOW])->count();
//        if($count>1){
//            $app_name = env('APP_ENV');
//            if (!App::environment('local')) {
//                Ding::text('预警信息.', [], true);
//                $markdown_text = "## 异常预警 \r\n#### 平台-**快闪卡贷{$app_name}**\r\n用户【{$loan->user_id}】进行中的授信条数异常 时间：{{$loan->created_at}}";
//                Ding::markdown([
//                    'title' => '预警啦!!!',
//                    'text' => $markdown_text,
//                ]);
//                SamLogFacade::debug("用户【{$loan->user_id}】进行中的授信条数异常 时间：{$loan->created_at}");
//            }
//        }
    }
}